'''
@author: pnuzhnyi
'''

from django.db import models
from djangotoolbox.fields import ListField
from lib.utils import Property
from django.utils import simplejson as json

"""
    conditions
        wave
        wind
        wtemp
"""

class event(models.Model):
    name = models.CharField(max_length=100)
    description = models.CharField(max_length=100)
    img = models.CharField(max_length=100)
    location = models.CharField(max_length=100)
    when = models.DateField()
    fee = models.IntegerField()
    prize = models.IntegerField(default=0)
    conditions_dump = models.CharField(max_length=100)
    participants = ListField(models.CharField(max_length=100),default=[])
    status = models.CharField(max_length=20,choices=(('pending','Pending'),('over','Over')),default='pending')
    
    @Property
    def conditions():
        def fget(self):
            return json.loads(self.conditions_dump)

        def fset(self, value):
            self.conditions_dump = json.dumps(value)

        def fdel(self):
            pass

        return locals()
    
    @Property
    def number_participants():
        def fget(self):
            return len(self.participants)

        def fset(self, value):
            pass

        def fdel(self):
            pass

        return locals()
    
class event_winner(models.Model):
    event_id = models.CharField(max_length=100)
    participant_id = models.CharField(max_length=100)
    place = models.IntegerField()
    updated = models.BooleanField(default=False)
    notified = models.BooleanField(default=False)
    compensation_dump = models.CharField(max_length=100)
    
    @Property
    def compensation():
        def fget(self):
            return json.loads(self.compensation_dump)

        def fset(self, value):
            self.compensation_dump = json.dumps(value)

        def fdel(self):
            pass

        return locals()